/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.processing.base.helpers.latex.tables;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Vector;
import org.apache.hadoop.hbase.util.Bytes;

/**
 *
 * @author nastya
 */
public class ColumnAddress {
    private byte[] family;
    private byte[] column;
    private final ColumnValueConverter converter;

    public ColumnAddress(String family, String column, ColumnValueConverter converter) {
        this.family = Bytes.toBytes(family);
        this.column = Bytes.toBytes(column);
        this.converter = converter;
    }

    public byte[] getColumn() {
        return column;
    }

    public byte[] getFamily() {
        return family;
    }
    public String getColumnStringRepresentation() {
        return Bytes.toString(column);
    }

    public String getFamilyStringRepresentation() {
        return Bytes.toString(family);
    }

    public String convertValue(byte[] bytes) {
        return converter.convert(bytes);
    }
    
    public static Vector<ColumnAddress> create(String family, ColumnValueConverter converter, String... columns){
        Vector<ColumnAddress> address = new Vector<ColumnAddress>();
        
        for (String columnName : columns) {
            address.add(new ColumnAddress(family, columnName, converter));
        }
        
        return address;
    }
    
    public static final ColumnValueConverter converterInt = new IntColumnValueConverter();
    public static final ColumnValueConverter converterLong = new LongColumnValueConverter();
    public static final ColumnValueConverter converterDouble = new DoubleColumnValueConverter();
    public static final ColumnValueConverter converterString = new StringColumnValueConverter();

    
    public interface ColumnValueConverter{
        public String convert(byte[] bytes);
    }
    
    private static class IntColumnValueConverter implements ColumnValueConverter{

        @Override
        public String convert(byte[] bytes) {
            return Bytes.toInt(bytes) + "";
        }
        
    }
    private static class LongColumnValueConverter implements ColumnValueConverter{

        @Override
        public String convert(byte[] bytes) {
            return Bytes.toLong(bytes) + "";
        }
        
    }
    private static class DoubleColumnValueConverter implements ColumnValueConverter{
        private static DecimalFormat f = new DecimalFormat("#.#########");

        @Override
        public String convert(byte[] bytes) {
            double value = Bytes.toDouble(bytes);
            return f.format(value);
        }
        
    }
    private static class StringColumnValueConverter implements ColumnValueConverter{

        private static final String[] quoteSymbol = 
                new String[]{"\\", "{",  "}",  "%",  "&",  "$",  "#",  "_", "^",  "~"};
        private static HashMap<String, String> replacement = new HashMap<String, String>();
        
        static{
            replacement.put("{", "\\{");
            replacement.put("}", "\\}");
            replacement.put("%", "\\%");
            replacement.put("&", "\\&");
            replacement.put("$", "\\$");
            replacement.put("#", "\\#");
            replacement.put("_", "\\_");
            replacement.put("^", "\\textasciicircum");
            replacement.put("~", "\\textasciitilde");
            replacement.put("\\", "\\textbackslash");
        }
        
        @Override
        public String convert(byte[] bytes) {
            String parsedString = Bytes.toString(bytes);
            for (String sym : quoteSymbol) {
                parsedString = parsedString.replace(sym, replacement.get(sym));
            }
            return parsedString;
        }
    
    }
}
